/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neosystem.services.impl;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;

import ar.com.greensoft.neosystem.dao.DaoException;
import ar.com.greensoft.neosystem.dao.UserDao;
import ar.com.greensoft.neosystem.model.Authority;
import ar.com.greensoft.neosystem.model.User;
import ar.com.greensoft.neosystem.services.Encoded;
import ar.com.greensoft.neosystem.services.Encoded.Algoithm;
import ar.com.greensoft.neosystem.services.PasswordEncoder;
import ar.com.greensoft.neosystem.services.ServiceException;
import ar.com.greensoft.neosystem.services.UserExistsException;
import ar.com.greensoft.neosystem.services.UserService;

/**
*
* @author Jorge E. Villaverde
* @since 1.0
*/
@Stateless
public class UserServiceImpl implements UserService {
	@Inject
	private Logger logger;
	
	@EJB
	private UserDao userDao;
	
	@Inject
	@Encoded(Algoithm.HEX)
	private PasswordEncoder passwordEncoder;

	public User loadUser(String username) throws ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Loading User:" + username);
		try {
			return userDao.findUserByUsername(username);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public void saveUser(User user) throws ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Saving User:" + user);
		try {
			userDao.merge(user);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public User findUserByUsername(String username) throws ServiceException  {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Searching Users with username:" + username);
		try {
			return userDao.findUserByUsername(username);
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
	}

	@Override
	public Collection<User> getUsers() throws ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Retrieving all Users");
		Collection<User> ret = null;
		try {
			ret = userDao.findAll();
			if(logger.isLoggable(Level.FINE))
				logger.fine((ret == null)? "No Users Found." : ret.size() + " Users Found");
			return ret;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public User createUser(String username, String password) throws UserExistsException, ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Creating User: " + username);

		User user;
		try {
			user = userDao.findUserByUsername(username);
			if(user != null)
				throw new UserExistsException(username);
			
			user = new User();
			user.setUsername(username);
			user.setPassword(passwordEncoder.encodePassword(password));
			user.setEnabled(true);
			
			userDao.persist(user);
			return user;		
		} catch (DaoException e) {
			logger.severe(e.getMessage());
			throw new ServiceException(e);
		}		
	}

	@Override
	public void grantAuthority(Authority authority) throws ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Adding Authority: " + authority);
		try {
			userDao.addAuthority(authority);
		} catch (DaoException e) {
			logger.severe(e.getMessage());
			throw new ServiceException(e.getMessage());
		}		
		
	}
	
	@Override
	public void revokeAuthority(Authority authority) throws ServiceException {
		if(logger.isLoggable(Level.FINE))
			logger.fine("Adding Authority: " + authority);
		try {
			userDao.removeAuthority(authority);
		} catch (DaoException e) {
			logger.severe(e.getMessage());
			throw new ServiceException(e.getMessage());
		}		
		
	}
}